Thread: [Big Endian vs Little Endian] Review Problem

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    75

    [Big Endian vs Little Endian] Review Problem

    Assuming sizeof(int) = 4 ::

    My question is what 'y' would be following the code

    int x = 0x1234567;
    char *p = (char *)&x;
    int y = (int)(*p);

    (a) On a little endian machine
    (b) On a big endian machine

    ---------------------------------------------------------

    The answers are 103 for little endian and
    1 for big endian.


    However, I am confused how those are the answers. I understand in a little endian machine that the bits are stored from the least significant to the most, and vice-versa for big endian. Should I be converting x into binary at any point? And what is the purpose of the pointer being assigned to the address of x then being casted into an int? Any help would be nice, a little direction for me may help it click in my head.
    Last edited by Roadrunner2015; 02-16-2015 at 02:35 PM. Reason: formatting

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Roadrunner2015
    Should I be converting x into binary at any point?
    Notice that the integer literal is expressed in hexadecimal notation, yet the answers were given in decimal representation. I would expect a conversion from hexadecimal representation to decimal representation instead of from hexadecimal to binary.

    Quote Originally Posted by Roadrunner2015
    And what is the purpose of the pointer being assigned to the address of x then being casted into an int?
    The idea is to examine the bytes of the int by reinterpreting it as an array of char. So, the cast of the address of x to char* reinterprets x as an array of char, where p points to the first char in this array. The cast is a cast of *p, not of p, i.e., to consider the first char in this array as an int.
    Last edited by laserlight; 02-16-2015 at 02:40 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Convert the value of 0x12 to decimal.
    Convert the value of 0x67 to decimal.

    And, recheck your answer for big endian because 1 is wrong.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stahta01
    Convert the value of 0x12 to decimal.
    Convert the value of 0x67 to decimal.

    And, recheck your answer for big endian because 1 is wrong.
    0x1234567 can be expressed as 0x01234567 in order for all four bytes in the integer literal to become apparent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Roadrunner2015 View Post
    However, I am confused how those are the answers. I understand in a little endian machine that the bits are stored from the least significant to the most, and vice-versa for big endian.
    Endian-ness determines the order of the bytes, not the bits. Since your initial value can be written as 0x01234567, and assuming your int type is big enough to contain that number, the most significant byte is 0x01, and the least is 0x67.

    Quote Originally Posted by Roadrunner2015 View Post
    Should I be converting x into binary at any point?
    Nope. Completely unnecessary.

    Quote Originally Posted by Roadrunner2015 View Post
    And what is the purpose of the pointer being assigned to the address of x then being casted into an int?
    A char, in C, is generally represented as a single byte. So taking the address of x and storing that in a char pointer, gives us a char pseudo-array of size 4 (the size is an assumption, in this example, for the sake of clarity). Therefore, dereferencing the pointer gives us the first element of the "array." We then convert that value to an int, and store it in y. It will give us the value at the lowest address where x resides. On a little-endian machine, it'll be the least significant byte, and on a big-endian machine, it'll be the most significant byte.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    75
    deleted
    Last edited by Roadrunner2015; 02-16-2015 at 02:49 PM. Reason: i replied late

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    75
    Quote Originally Posted by Elkvis View Post
    Endian-ness determines the order of the bytes, not the bits.
    This! Thank you. And I understand the reasoning for the whole (char *) deal, in my words it's to give rule on how this code should be read. The machines, I assume, write AND read in the same direction so without it the answers would be the same?

    Thank you guys so much. Big clarification and not something I believe I would catch in a simple review.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Roadrunner2015 View Post
    The machines, I assume, write AND read in the same direction
    A machine will always start reading OR writing, for a particular multi-byte value, at the lowest address of the value in memory. How it's handled after that is architecure-specific.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    11
    nice to know. thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fiddling with the bits(Big Endian vs Little Endian)
    By A34Chris in forum C Programming
    Replies: 71
    Last Post: 11-26-2012, 10:39 PM
  2. Little endian and Bit endian advantages and disadvantages
    By nkrao123@gmail. in forum C Programming
    Replies: 4
    Last Post: 09-11-2011, 04:40 AM
  3. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 06:47 PM
  4. Big Endian Little Endian Complex- Converting Characters
    By bd02eagle in forum C Programming
    Replies: 3
    Last Post: 07-11-2006, 01:01 AM
  5. Big endian/Little endian conversion
    By bonkey in forum Windows Programming
    Replies: 5
    Last Post: 09-25-2002, 02:29 PM